<!DOCTYPE HTML>
<html>
	<head>
		<meta charset="utf-8">
		<title>[(${dto.menu.title})]</title>
		<meta name="renderer" content="webkit">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="format-detection" content="telephone=no">
		<link rel="icon" type="image/x-icon" href="/static/img/favicon.ico">
		<!-- jQuery-->
		<script type="text/javascript" src="/static/plugins/jquery/jquery-3.6.0.min.js"></script>
		<!-- layui-->
		<link rel="stylesheet" href="/static/plugins/layui/2.7.6/css/layui.css" media="all">
		<script src="/static/plugins/layui/2.7.6/layui.js" charset="utf-8"></script>

		<!-- fullcalendar-->
		<link rel="stylesheet" href="/static/plugins/fullcalendar/css/fullcalendar_tpHoliday_Set.css" media="all" >
		<script src='/static/plugins/fullcalendar/js/fullcalendar.min.js'></script>
		<style type="text/css">
			#calendar{
				width:960px;
				margin:20px auto 10px auto;
				background-color: white;
				padding: 20px 20px 20px 20px;
			}
			body{font-size:12px;}
			a{color:#666;text-decoration:none;}
		</style>

		<script type="text/javascript">
			$(function() {
				$('#calendar').fullCalendar({
					header: {
						left: 'prevYear,prev today next,nextYear',
						center: 'title',
						right: 'month,agendaWeek',
					},
					editable: false,
					eventLimit: true,
					navLinks: true,
					locale: 'zh-cn',
					businessHours: true,
					firstDay:1,
					//events: eventsInfo,
					//通过鼠标点击日历某一天触发dayClick的callback，参数date为点击的日期或时间，常用于为某一天或为某一时刻添加日程，具体方法如下（使用Console日志打印点击的日期或时间）
					dayClick : function( date, allDay,  jsEvent, view) {
						var setDate = new Date(date);
						var yearNum = setDate.getFullYear();
						var monthNum = setDate.getMonth() + 1;
						var dayNum = setDate.getDate();
						var week = setDate.getDay();
						var weeks = ["7", "1", "2", "3", "4", "5", "6"];
						var weeksNum = weeks[week];

						var timeStr = yearNum + '-' + (monthNum<10?"0"+monthNum:monthNum) + '-' + (dayNum<10?"0"+dayNum:dayNum);
						var events = $('#calendar').fullCalendar('clientEvents');
						var title = "";
						var id = "";
						//var index = -1;
						for(var i=0;i<events.length;i++){
							var evenDate = new Date(events[i].start);
							var thisyearNum = evenDate.getFullYear();
							var thismonthNum = evenDate.getMonth() + 1;
							var thisdayNum = evenDate.getDate();
							var thistimeStr = thisyearNum + '-' + (thismonthNum<10?"0"+thismonthNum:thismonthNum) + '-' + (thisdayNum<10?"0"+thisdayNum:thisdayNum);
							if(timeStr==thistimeStr){
								title = events[i].title;
								id = events[i].id;
								//index = i;
							}
						}
						var msg = "";
						if(id!=null && id!=''){
							msg = (title=='假期'?'工作日':'假期');
						}else {
							msg = ((weeksNum=='7' || weeksNum=='6')?'工作日':'假期');
						}

						layer.confirm('确定要将'+timeStr+'设置为  '+msg+'  ?', {icon: 3}, function () {
							//layer.msg('点击确定的回调', {icon: 1});
							//点击确定的回调
							layer.msg('修改中', {
								icon: 16,
								shade: 0.01
							});
							changeHolidaySet(id,msg,timeStr);
						}, function () {});//点击取消的回调


					},
					//通过鼠标点击日历某一Event数据触发eventClick的callback，参数event表示被点击的Event
					eventClick: function(event, jsEvent, view) {
						var setDate = new Date(event.start);
						var yearNum = setDate.getFullYear();
						var monthNum = setDate.getMonth() + 1;
						var dayNum = setDate.getDate();
						var week = setDate.getDay();
						var weeks = ["7", "1", "2", "3", "4", "5", "6"];
						var weeksNum = weeks[week];
						var timeStr = yearNum + '-' + (monthNum<10?"0"+monthNum:monthNum) + '-' + (dayNum<10?"0"+dayNum:dayNum);

						var title = event.title;
						var id = event.id;

						var msg = "";
						if(id!=null && id!=''){
							msg = (title=='假期'?'工作日':'假期');
						}else {
							msg = ((weeksNum=='7' || weeksNum=='6')?'工作日':'假期');
						}

						layer.confirm('确定要将'+timeStr+'设置为  '+msg+'  ?', {icon: 3}, function () {
							//layer.msg('点击确定的回调', {icon: 1});
							//点击确定的回调
							layui.layer.msg('修改中', {
								icon: 16,
								shade: 0.01
							});
							changeHolidaySet(id,msg,timeStr);
						}, function () {});//点击取消的回调

					},
				});

				var eventsInfo = [];

				$.ajax({
					url : '[(${dto.menu.pathGridRequest})]',
					data : {},
					type : 'POST',
					async : false,
					success : function(result) {
						if (result.code == '0') {
							var resDate = result.data;
							for(var i=0;i<resDate.length;i++){
								//var setDate = resDate[i].setDate;
								var setDate = new Date(resDate[i].setDate);
								var yearNum = setDate.getFullYear();
								var monthNum = setDate.getMonth() + 1;
								var dayNum = setDate.getDate();
								var timeStr = yearNum + '-' + (monthNum<10?"0"+monthNum:monthNum) + '-' + (dayNum<10?"0"+dayNum:dayNum);
								eventsInfo.push({
									id:resDate[i].id,
									title: (resDate[i].isHoliday=="0"?'假期':'工作日'),
									start: timeStr,
									color : (resDate[i].isHoliday=="0"?'#89D961':'#5A88FE'),//边框和背景颜色
									allDay: true,
								});
							}
							//清除原来加载进去的数据,不然会出现很多重复加载的数据
							$("#calendar").fullCalendar('removeEvents');
							//添加json对象
							$.each(eventsInfo, function (index, term) {
								$("#calendar").fullCalendar('renderEvent', term, true);
							});
						}else{
							layer.msg(result.msg);
						}
					}
				});

				//更新假期设置
				function changeHolidaySet(id,msg,timeStr) {
					var setStatus = (msg=='假期'?'0':'1');
					$.ajax({
						url: '/web/business/tpHolidaySet/setHolidaySetMsg',
						data: {
							'setDate':timeStr,
							'isHoliday':setStatus,
							'id':((id!=null &&id!='')?id:'')
						},
						type: 'POST',
						async: true,
						success: function (result) {
							if(result.code=='0'){
								if(id!=null &&id!=''){//修改
									for(var i=0;i<eventsInfo.length;i++){
										var evenDate = new Date(eventsInfo[i].start);
										var thisyearNum = evenDate.getFullYear();
										var thismonthNum = evenDate.getMonth() + 1;
										var thisdayNum = evenDate.getDate();
										var thistimeStr = thisyearNum + '-' + (thismonthNum<10?"0"+thismonthNum:thismonthNum) + '-' + (thisdayNum<10?"0"+thisdayNum:thisdayNum);
										if(timeStr==thistimeStr){
											eventsInfo[i].title = msg;
											eventsInfo[i].color = (msg=='假期'?'#89D961':'#5A88FE');
										}
									}
								}else {//新增
									eventsInfo.push({
										id:result.msg,
										title: msg,
										start: timeStr,
										color : (msg=='假期'?'#89D961':'#5A88FE'),//边框和背景颜色
										allDay: true,
									});
								}
								//清除原来加载进去的数据,不然会出现很多重复加载的数据
								//$("#calendar").fullCalendar('removeEvents');
								//添加json对象
								$.each(eventsInfo, function (index, term) {
									$("#calendar").fullCalendar('renderEvent', term, true);
								});
								layer.close();
								layer.msg("设置成功", {icon: 1});
							}else {//失败
								layer.close();
								layer.msg(result.msg, {icon: 2});
							}
						},
						error: function () {
							layer.close();
							layer.msg("假期设置失败，请稍后再试！", {icon: 2});
						}
					});
				}
			});

		</script>
	</head>

<body>
<div id="main" style="width:1060px;margin: 0 auto;">

	<div id='calendar' style="margin: 0 auto;"></div>
</div>

</body>
</html>